//
//  XGBaseListContainerViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/4/1.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import JXSegmentedView

class XGBaseListContainerViewController: UIViewController, JXSegmentedViewDelegate, JXSegmentedListContainerViewDataSource
{
    // MARK: - 控制器生命周期方法
    
    override func loadView()
    {
        super.loadView()
        setUpUI()
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        loadCategories { (titles) in
            self.segmentedDataSource.titles = titles
            self.segmentedView.reloadData()
        }
    }
    
    // MARK: - 公开方法
    
    /// 加载分类标题
    func loadCategories(completion: @escaping ([String]) -> Void) -> Void {}
        
    // MARK: - 私有属性
    
    /// 视图模型
    private let viewModel = XGHomeViewModel()
    /// 指示器
    private lazy var segmentedView = JXSegmentedView()
    /// 容器
    private lazy var listContainerView = JXSegmentedListContainerView(dataSource: self, type: .collectionView)
    /// 指示器数据源 必须属性持有
    private let segmentedDataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.isTitleColorGradientEnabled = true
        dataSource.isTitleZoomEnabled = true
        dataSource.itemSpacing = 30.0
        return dataSource
    }()
}

// MARK: - JXSegmentedViewDelegate

extension XGBaseListContainerViewController
{
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {}
}

// MARK: - JXSegmentedListContainerViewDataSource

extension XGBaseListContainerViewController
{
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int { return 0 }
       
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate { return XGContainerView()}
}

// MARK: - 私有扩展

private extension XGBaseListContainerViewController
{
    func setUpUI() -> Void
    {
        view.backgroundColor = UIColor.white
        view.addSubview(segmentedView)
        view.addSubview(listContainerView)
        segmentedView.frame = CGRect(x: 0, y: TodayNews_NavigationBar_Height, width: TodayNews_Screen_Width, height: 44)
        listContainerView.frame = CGRect(x: 0, y: segmentedView.frame.maxY, width: view.width, height: TodayNews_Screen_Height - segmentedView.frame.maxY - TodayNews_TabBar_Height)
        
        // 配置指示器
        segmentedView.theme_backgroundColor = "colors.home_category_background"
        segmentedView.dataSource = segmentedDataSource
        segmentedView.delegate = self
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = JXSegmentedViewAutomaticDimension
        indicator.lineStyle = .lengthenOffset
        indicator.verticalOffset = 3
        segmentedView.indicators = [indicator]
        // 配置容器
        listContainerView.backgroundColor = UIColor.white
        segmentedView.listContainer = listContainerView
    }
}

// MARK: - XGContainerView 临时返回无意义

private class XGContainerView: UIView, JXSegmentedListContainerViewListDelegate
{
    func listView() -> UIView
    {
        return self
    }
}
